import api from '@/utils/request'
import { USER, AUTH, CASE, LAWYER, SYSTEM, UPLOAD } from '@/config/api-endpoints'

// 用户相关 API (对应 UserController)
export const userAPI = {
  // 用户登录
  login: (credentials) => api.post(USER.LOGIN, credentials),
  
  // 用户注册/创建
  create: (userData) => api.post(USER.CREATE, userData),
  
  // 更新用户信息
  update: (userData) => api.put(USER.UPDATE, userData),
  
  // 获取用户列表
  getList: () => api.get(USER.LIST),
  
  // 分页查询用户
  getPage: (pageData) => api.get(USER.PAGE, pageData),
  
  // 获取用户详情
  getInfo: (id) => api.get(USER.GET_INFO(id)),
  
  // 删除用户
  delete: (id) => api.delete(USER.DELETE(id))
}

// 用户认证相关 API (兼容旧版本)
export const authAPI = {
  // 用户登录
  login: (credentials) => api.post(AUTH.LOGIN, credentials),
  
  // 用户注册
  register: (userData) => api.post(AUTH.REGISTER, userData),
  
  // 用户登出 (后端可能没有此接口)
  logout: () => api.post(AUTH.LOGOUT),
  
  // 刷新 Token (后端可能没有此接口)
  refreshToken: () => api.post(AUTH.REFRESH_TOKEN),
  
  // 验证 Token (后端可能没有此接口)
  verifyToken: () => api.post(AUTH.VERIFY_TOKEN),
  
  // 获取用户信息 (使用用户详情接口)
  getUserInfo: (id) => api.get(USER.GET_INFO(id)),
  
  // 更新用户信息
  updateUserInfo: (userData) => api.put(USER.UPDATE, userData),
  
  // 更新用户头像 (需要后端实现)
  updateAvatar: (file) => api.upload(UPLOAD.AVATAR, file),
  
  // 更新用户设置 (需要后端实现)
  updateSettings: (settings) => api.put('/api/user/settings', settings),
  
  // 修改密码 (需要后端实现)
  changePassword: (passwordData) => api.put('/api/user/password', passwordData)
}

// 案件管理相关 API (需要后端实现)
export const caseAPI = {
  // 获取案件列表
  getCaseList: (params) => api.get(CASE.LIST, params),
  
  // 获取案件详情
  getCaseDetail: (caseId) => api.get(CASE.DETAIL(caseId)),
  
  // 创建案件
  createCase: (caseData) => api.post(CASE.CREATE, caseData),
  
  // 更新案件
  updateCase: (caseId, caseData) => api.put(CASE.UPDATE(caseId), caseData),
  
  // 删除案件
  deleteCase: (caseId) => api.delete(CASE.DELETE(caseId)),
  
  // 上传案件材料
  uploadMaterial: (caseId, file, onProgress) => 
    api.upload(CASE.MATERIALS(caseId), file, onProgress),
  
  // 获取案件统计
  getCaseStatistics: () => api.get(CASE.STATISTICS),
  
  // 分配案件
  assignCase: (caseId, lawyerId) => api.post(CASE.ASSIGN(caseId), { lawyerId }),
  
  // 更新案件状态
  updateCaseStatus: (caseId, status) => api.put(CASE.STATUS(caseId), { status })
}

// 律师管理相关 API (需要后端实现)
export const lawyerAPI = {
  // 获取律师列表
  getLawyerList: (params) => api.get(LAWYER.LIST, params),
  
  // 获取律师详情
  getLawyerDetail: (lawyerId) => api.get(LAWYER.DETAIL(lawyerId)),
  
  // 创建律师信息
  createLawyer: (lawyerData) => api.post(LAWYER.CREATE, lawyerData),
  
  // 更新律师信息
  updateLawyer: (lawyerId, lawyerData) => api.put(LAWYER.UPDATE(lawyerId), lawyerData),
  
  // 删除律师信息
  deleteLawyer: (lawyerId) => api.delete(LAWYER.DELETE(lawyerId)),
  
  // 验证律师资格
  verifyLawyer: (lawyerId, verifyData) => api.post(LAWYER.VERIFY(lawyerId), verifyData),
  
  // 获取律师专业领域
  getLawyerSpecialties: () => api.get(LAWYER.SPECIALTIES)
}

// 系统相关 API (需要后端实现)
export const systemAPI = {
  // 获取系统统计
  getStatistics: () => api.get(SYSTEM.STATISTICS),
  
  // 获取系统配置
  getSystemConfig: () => api.get(SYSTEM.CONFIG),
  
  // 获取菜单配置
  getMenuConfig: () => api.get(SYSTEM.MENU),
  
  // 系统健康检查
  healthCheck: () => api.get(SYSTEM.HEALTH),
  
  // 文件上传
  uploadFile: (file, onProgress) => api.upload(UPLOAD.FILE, file, onProgress),
  
  // 图片上传
  uploadImage: (file, onProgress) => api.upload(UPLOAD.IMAGE, file, onProgress),
  
  // 文档上传
  uploadDocument: (file, onProgress) => api.upload(UPLOAD.DOCUMENT, file, onProgress),
  
  // 头像上传
  uploadAvatar: (file, onProgress) => api.upload(UPLOAD.AVATAR, file, onProgress)
}

// 导出所有 API
export default {
  user: userAPI,
  auth: authAPI,
  case: caseAPI,
  lawyer: lawyerAPI,
  system: systemAPI
}

// 统一处理成功响应（兼容旧版本）
export const handleSuccess = (response, action) => {
  // 记录成功日志（开发环境）
  if (process.env.NODE_ENV === 'development') {
    console.log(`✔ ${action}成功`, response.data)
  }

  if (response.data.code !== 200) {
    console.error(response.data.message)
  }

  // 可在此处添加统一数据处理
  return response.data
}

// 统一处理错误响应（兼容旧版本）
export const handleError = (error, action, userMessage) => {
  // 记录错误日志
  console.error(`✖ ${action}失败`, error)
  // 展示用户提示
  console.error(userMessage || '操作失败，请稍后重试')
  // 将错误继续抛出以便后续处理
  return Promise.reject(error)
}